from celery import shared_task
from django.utils import timezone
from .models import Post


@shared_task
def publish_scheduled_posts():
    """定时发布博客文章的任务"""
    now = timezone.now()
    scheduled_posts = Post.objects.filter(
        status='scheduled',
        publish_date__lte=now
    )

    for post in scheduled_posts:
        post.status = 'published'
        post.save(update_fields=['status'])

    return f"已发布 {scheduled_posts.count()} 篇定时文章"

